package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.icanci.loopstack.bic.biz.dts.GraphDtsServiceProxy;
import cn.icanci.loopstack.bic.biz.mapper.EdgeStyleMapper;
import cn.icanci.loopstack.bic.biz.service.EdgeStyleService;
import cn.icanci.loopstack.bic.biz.utils.TemplateFileUtils;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.EdgeStyleVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.EdgeStyleDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.EdgeStyleDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 22:45
 */
@Service
public class EdgeStyleServiceImpl implements EdgeStyleService {

    @Resource
    private EdgeStyleDAO         edgeStyleDAO;
    @Resource
    private EdgeStyleMapper      edgeStyleMapper;
    @Resource
    private GraphDtsServiceProxy graphDtsServiceProxy;

    @Override
    public List<EdgeStyleVO> queryAll() {
        return edgeStyleMapper.dos2vos(edgeStyleDAO.queryAll());
    }

    @Override
    public void save(EdgeStyleVO edgeStyleVO) {
        // TODO 日志记录
        if (doInsert(edgeStyleVO)) {
            EdgeStyleDO insert = edgeStyleMapper.vo2do(edgeStyleVO);
            edgeStyleDAO.insert(insert);
        } else {
            edgeStyleDAO.update(edgeStyleMapper.vo2do(edgeStyleVO));
            graphDtsServiceProxy.dtsUnitViewTree(edgeStyleVO);
        }
        // TODO 最后补充更新视图的逻辑，因为这个地方会构建缓存
    }

    @Override
    public EdgeStyleVO queryById(String id) {
        return edgeStyleMapper.do2vo(edgeStyleDAO.queryOneById(id));
    }

    @Override
    public PageList<EdgeStyleVO> queryPage(EdgeStyleVO edgeStyleVO, int pageNum, int pageSize) {
        PageList<EdgeStyleDO> pageQuery = edgeStyleDAO.pageQuery(edgeStyleMapper.vo2do(edgeStyleVO), pageNum, pageSize);
        return new PageList<>(edgeStyleMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());

    }

    @Override
    public EdgeStyleVO queryBySystemUuidAndName(String systemUuid, String name) {
        return edgeStyleMapper.do2vo(edgeStyleDAO.queryBySystemUuidAndName(systemUuid, name));
    }

    @Override
    public EdgeStyleVO queryBySystemUuidAndKey(String systemUuid, String key) {
        return edgeStyleMapper.do2vo(edgeStyleDAO.queryBySystemUuidAndKey(systemUuid, key));
    }

    @Override
    public List<TextValue> loadSelector(String systemUuid) {
        List<EdgeStyleVO> nodeStyles = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (EdgeStyleVO nodeStyle : nodeStyles) {

            if (!StringUtils.equals(systemUuid, nodeStyle.getSystemUuid())) {
                continue;
            }

            String label;
            if (isDeleted(nodeStyle)) {
                label = String.format(DELETED_FORMAT, nodeStyle.getEdgeStyleName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, nodeStyle.getEdgeStyleName());
            }
            String value = nodeStyle.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public String loadDefaultStyle() {
        return TemplateFileUtils.queryDefaultEdgeStyleTemplate();
    }

    @Override
    public EdgeStyleVO queryByUuid(String uuid) {
        return edgeStyleMapper.do2vo(edgeStyleDAO.queryByUuid(uuid));
    }
}
